package com.hns.can.report.web;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.common.domain.GridPage;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.CommEnerConsStatInfoEntity;
import com.hns.can.master.service.TendencyChartsDataTools;
import com.hns.can.report.entity.BiCarIdxInfoEntity;
import com.hns.can.report.service.CarAnaService;
import com.hns.can.report.service.DrvAnaService;
import com.hns.can.report.vo.CarAnaCarVo;
import com.hns.can.report.vo.DrivingRecordAnalysisVo;
import com.hns.can.report.vo.DrvAnaDrvVo;
import com.hns.frame.mybatis.Pagination;

/**
 * 车辆画像控制类
 * 
 * @author:lan
 * @email:763836737@qq.com
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/carAna")
public class CarAnaController extends BaseController {
    /**
     * 车辆画像service
     */
    @Autowired
    private CarAnaService carAnaService;
    @Autowired
    private DrvAnaService drvAnaService;
    @Autowired
    private TendencyChartsDataTools tendencyChartsDataTools;
    /**
     * 根据车辆id查询车辆信息
     * 
     * @param carId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarInfoByCarId")
    @ResponseBody
    public Object getCarInfoByCarId(@RequestParam(value = "carId") String carId) throws Exception {
        CarAnaCarVo vo = carAnaService.getCarAnaCarInfo(carId);
        return this.getReturnResult(vo);
    }
    
    /**
     * 根据车辆id查询关联驾驶员
     * 
     * @param carId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarDrvRel")
    @ResponseBody
    public Object getCarDrvRel(@RequestParam(value = "carId") String carId) throws Exception {
        BaseEntity entity = carAnaService.getCarDrvRel(carId);
        return this.getReturnResult(entity);
    }
    
    /**
     * 查询故障数据
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarAnaIdx")
    @ResponseBody
    public Object getCarAnaIdx(CarAnaCarVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"040101","040102","040103"});
        List<BiCarIdxInfoEntity> list = carAnaService.getCarAnaIdx(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 故障排名
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarAnaRank")
    @ResponseBody
    public Object getCarAnaRank(CarAnaCarVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"040301","040402"});
        List<Map<String,Object>> list = carAnaService.getCarAnaRank(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 车辆故障趋势图
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarAnaTrend")
    @ResponseBody
    public Object getCarAnaTrend(CarAnaCarVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"040201"});
        List<Map<String,Object>> list = carAnaService.getCarAnaTrend(vo);
        list = tendencyChartsDataTools.fillMultiTendencyData(list, vo.getDateType());
        return this.getReturnResult(list);
    }
    
    /**
     * 能耗趋势图
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarAnaEnerTrend")
    @ResponseBody
    public Object getCarAnaEnerTrend(CarAnaCarVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"040601","040602","040701"});
        List<Map<String,Object>> list = carAnaService.getCarAnaTrend(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 车辆里程趋势图
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarAnaMileTrend")
    @ResponseBody
    public Object getCarAnaMileTrend(CarAnaCarVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"040601","040602"});
        List<Map<String,Object>> list = carAnaService.getCarAnaTrend(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 能耗排名
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarAnaEnerRank")
    @ResponseBody
    public Object getCarAnaEnerRank(CarAnaCarVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"040801","040802","040803"});
        List<Map<String,Object>> list = carAnaService.getCarAnaEnergyRank(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 能耗转速分析
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarAnaEnergySpdAna")
    @ResponseBody
    public Object getCarAnaEnergySpdAna(CarAnaCarVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"040901","040902","040903"});
        List<Map<String,Object>> list = carAnaService.getCarAnaEnergySpdAna(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 查询 能耗数据
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarAnaEnerIdx")
    @ResponseBody
    public Object getCarAnaEnerIdx(CarAnaCarVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"040501","040502","040503","040504","040505","040506","040507","040508","040509","040510"});
        List<BiCarIdxInfoEntity> list = carAnaService.getCarAnaIdx(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 查询行为数据
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarAnaBehIdx")
    @ResponseBody
    public Object getCarAnaBehIdx(CarAnaCarVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"041001","041002"});
        List<BiCarIdxInfoEntity> list = carAnaService.getCarAnaIdx(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 行为趋势图
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarAnaBehTrend")
    @ResponseBody
    public Object getCarAnaBehTrend(CarAnaCarVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"041101","041102","041103","041104","041105","041106","041107","041108","041109v","041110","041111","041112","041113","041114","041199"});
        List<Map<String,Object>> list = carAnaService.getCarAnaTrend(vo);
        list = tendencyChartsDataTools.fillMultiTendencyData(list, vo.getDateType());
        return this.getReturnResult(list);
    }
    
    /**
     * 行为排名
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarAnaBehRank")
    @ResponseBody
    public Object getCarAnaBehRank(CarAnaCarVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"041201","041202"});
        List<Map<String,Object>> list = carAnaService.getCarAnaRank(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 查询维保数据
     * 
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarAnaMainteIdx")
    @ResponseBody
    public Object getCarAnaMainteIdx(CarAnaCarVo vo) throws Exception {
        vo.setIdxTypeArr(new String[]{"041301","041302","041303","041304","041305"});
        vo.setDateType("02");
        List<BiCarIdxInfoEntity> list = carAnaService.getCarAnaIdx(vo);
        return this.getReturnResult(list);
    }
    
    /**
     * 查询维保记录
     * @param dataVo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getMaintenInfoPage")
    @ResponseBody
    public Object searchMaintenInfoPage(CarAnaCarVo vo) throws Exception {
    	//02.昨天 03.近7天 04.本月 05.上月 06.今年 07.去年
    	DrvAnaDrvVo dvo = new DrvAnaDrvVo();
        if("02".equals(vo.getDateType())){
        	dvo.setTmpDateStr("to_char(sysdate-1,'yyyy-mm-dd')BEGIN_ATTN_TIME,to_char(sysdate,'yyyy-mm-dd')END_ATTN_TIME ");
        }else if("03".equals(vo.getDateType())){
        	dvo.setTmpDateStr("to_char(sysdate-7,'yyyy-mm-dd')BEGIN_ATTN_TIME,to_char(sysdate-1,'yyyy-mm-dd')END_ATTN_TIME ");
        }else if("04".equals(vo.getDateType())){
        	dvo.setTmpDateStr("to_char(sysdate,'yyyy-mm')||'-01'BEGIN_ATTN_TIME,to_char(sysdate-1,'yyyy-mm-dd')END_ATTN_TIME ");
        }else if("05".equals(vo.getDateType())){
        	dvo.setTmpDateStr("to_char(add_months(sysdate,-1),'yyyy-mm')||'-01'BEGIN_ATTN_TIME,to_char(add_months(sysdate,-1),'yyyy-mm')||'-31'END_ATTN_TIME ");
        }else if("06".equals(vo.getDateType())){
        	dvo.setTmpDateStr("to_char(sysdate,'yyyy')||'-01-01'BEGIN_ATTN_TIME,to_char(sysdate-1,'yyyy-mm-dd')END_ATTN_TIME");
        }else if("07".equals(vo.getDateType())){
        	dvo.setTmpDateStr("to_char(add_months(sysdate,-12),'yyyy')||'-01-01'BEGIN_ATTN_TIME,to_char(add_months(sysdate,-12),'yyyy')||'-12-31'END_ATTN_TIME ");
        }
        DrvAnaDrvVo timeVo = drvAnaService.getAttnTime(dvo);
        timeVo.setCarId(vo.getCarId());
        Pagination page = carAnaService.searchMainteInfoPage(timeVo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 查询行车记录分析
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping("/getRecordAnalysisList")
    @ResponseBody
    public Object getRecordAnalysisList(CarAnaCarVo vo) throws Exception {
        List<DrivingRecordAnalysisVo> list = carAnaService.searchRecordAnalysisList(vo);
        return this.getReturnResult(list);
    }
    
    /** 
     * 查询行车记录分析图表
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping("/getRecordAnalysisChart")
    @ResponseBody
    public Object getRecordAnalysisChart(CarAnaCarVo vo) throws Exception {
        List<CommEnerConsStatInfoEntity> list = carAnaService.searchRecordAnalysisChart(vo);
        return this.getReturnResult(list);
    }
}
